//
//  ThemeViewController.m
//  TravelToHelp
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 冯贵宾. All rights reserved.
//

#import "ThemeViewController.h"
#import "LASHeaderView.h"
#import "ThemeTableViewController.h"
@interface ThemeViewController ()<UIScrollViewDelegate>

@property (nonatomic, assign) CGPoint center;
@property (nonatomic, strong) LASHeaderView *naviView;
@property (nonatomic, strong) UIScrollView *themeScrollView;
@property (nonatomic, strong) ThemeTableViewController *themeVC;


@end

@implementation ThemeViewController



#pragma mark - life cycle -

- (void)viewDidLoad {
    [super viewDidLoad];
//     self.view.backgroundColor = [UIColor orangeColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self addCustomNavigationBar];
    [self addThemeScrollView];

}




- (void)viewDidDisappear:(BOOL)animated
{
    self.themeScrollView.contentOffset = CGPointMake(0, 0);    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



#pragma mark - delegate -


#pragma mark - event response -
- (void)actionRadioB:(UIButton *)sender
{
    self.themeScrollView.contentOffset = CGPointMake(kScreenWidth, 0);
    self.naviView.motionView.center = CGPointMake(self.center.x + 120, self.center.y);
    [self.naviView.videoB setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [self.naviView.radioB setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
}
- (void)actionVideoB:(UIButton *)sender
{
    self.themeScrollView.contentOffset = CGPointMake(0, 0);
    self.naviView.motionView.center = self.center;
    [self.naviView.radioB setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [self.naviView.videoB setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
}

#pragma mark - methods -
// 添加自定义navigationBar
- (void)addCustomNavigationBar
{
    // 自定义导航栏背景颜色
    // self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"TestNaviBar1.jpg"] forBarMetrics:(UIBarMetricsDefault)];
    self.naviView = [[LASHeaderView alloc] initWithFrame:CGRectMake((self.view.width - 240) / 2, 20, 240, 28)];
    self.naviView.layer.cornerRadius = 14;
    self.naviView.layer.masksToBounds = YES;
    self.naviView.alpha = 0.5;
    self.naviView.layer.borderWidth = 0.5;
    self.naviView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.center = self.naviView.motionView.center;
    [self.naviView.radioB addTarget:self action:@selector(actionRadioB:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.naviView.videoB addTarget:self action:@selector(actionVideoB:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.titleView = self.naviView;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat dx = scrollView.contentOffset.x * 120 / 375 ;
    
    CGPoint tempCenter = self.center;;
    tempCenter.x = self.center.x + dx;
    self.naviView.motionView.center = tempCenter;
    if (scrollView.contentOffset.x > 0) {
        [self.naviView.videoB setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [self.naviView.radioB setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    } else {
        [self.naviView.radioB setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [self.naviView.videoB setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];

    }
    
}



#pragma mark - setters and getters -
- (void)addThemeScrollView
{
    self.themeScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 49)];
//    self.themeScrollView.backgroundColor = [UIColor grayColor];
    self.themeScrollView.contentSize = CGSizeMake(kScreenWidth * 2, 0);
    // 取消边界回弹
    self.themeScrollView.bounces = NO;
    // 一滑一页
    self.themeScrollView.pagingEnabled = YES;
    self.themeScrollView.showsHorizontalScrollIndicator = NO;
    
    [self addChildViewControllers];
    self.themeVC.view.frame = self.themeScrollView.frame;

    [self.themeScrollView addSubview:self.themeVC.view];
    [self addChildViewController:self.themeVC];
    
    self.themeScrollView.delegate = self;
    [self.view addSubview:self.themeScrollView];
    
}

// 添加子控制器
- (void)addChildViewControllers
{
    self.themeVC = [[ThemeTableViewController alloc] init];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
